<template>
  <CommonPage title="车次定义">
    <template #mid>
      <SearchForm>
        <template #extraButtons>
          <a-button type="primary" @click="showCreateModal">新增车次</a-button>
        </template>
      </SearchForm>
    </template>
    <SlTable :columns="columns" row-key="id" :data-source="tableData" :pagination="pagination" :loading="tableLoading">
      <template #bodyCell="{ column, record }: BodyCellOptions<Partial<TrainNumberItem>>">
        <div v-if="column.key === 'oper'">
          <a @click="showUpdateModal(record)">编辑</a>
          <a class="ml-2 text-red-400" @click="handleDelete(record)">删除</a>
        </div>
      </template>
    </SlTable>
    <CURDDialog
      ref="CURDModalIns"
      id-key="id"
      :columns="columns"
      create-title="新增车次"
      update-title="编辑车次"
      delete-message="确认删除该车次吗？"
      :show-options="showOptions"
      :model="model"
      :model-form-setting="modelFormSetting"
      :table-data="tableData"
      :curd="curd"
      :reload-table="reloadTable"
      @before-ok="beforeOk"
      @open="open"
    />
  </CommonPage>
</template>
<script lang="ts" setup>
import { ref, shallowRef } from "vue";
import { useI18n } from "/@/locales/useI18n";
import { useCURD } from "/@/hooks/useCURD";
import { FormItemComponentType, CURDAction, CURDDialogExpose } from "/@/components/CURDDialog/models";
import type { FormItemData, OperItem, ShowCURDModalOptions, CURDModalEvent } from "/@/components/CURDDialog/models";
import type { TableProps } from "ant-design-vue";
import { PatternKey } from "/@/utils/verify";
import { loopItem } from "/@/utils/treeData";
import type { BodyCellOptions } from "/@/types/antd";
import { Api } from "/@/api/main/pis";
import type { TrainNumberItem } from "/@/api/main/pis/models";

const { t } = useI18n();
const columns = ref<TableProps<Partial<TrainNumberItem>>["columns"]>([
  {
    title: "车牌",
    dataIndex: "licensePlate",
  },
  {
    title: "车辆品牌",
    dataIndex: "vehicleBrand",
  },
  {
    title: "车辆型号",
    dataIndex: "vehicleModel",
  },
  {
    title: "车辆类型",
    dataIndex: "vehicleType",
  },
  {
    title: "车辆尺寸",
    dataIndex: "vehicleSize",
  },
  {
    title: "载货容积",
    dataIndex: "cargoCapacity",
  },
  {
    title: "载货重量",
    dataIndex: "cargoWeight",
  },
  {
    title: "行驶里程",
    dataIndex: "mileageDriven",
  },
  {
    title: t("common.oper"),
    key: "oper",
    width: 100,
  },
]);
const model = ref<Partial<TrainNumberItem>>({
  licensePlate: "",
  vehicleBrand: "",
  vehicleModel: "",
  vehicleType: "",
  vehicleSize: "",
  cargoCapacity: "",
  cargoWeight: "",
  mileageDriven: "",
});
const modelFormSetting: FormItemData<Partial<TrainNumberItem>>[] = [
  {
    key: "licensePlate",
    required: true,
  },
  {
    key: "vehicleBrand",
  },
  {
    key: "vehicleModel",
  },
  {
    key: "vehicleType",
  },
  {
    key: "vehicleSize",
  },
  {
    key: "cargoCapacity",
  },
  {
    key: "cargoWeight",
  },
  {
    key: "mileageDriven",
  },
];

const CURDModalIns = shallowRef<CURDDialogExpose>();
const showOptions = ref<ShowCURDModalOptions>();

const { pagination, tableData, tableLoading, curd, reloadTable } = useCURD<TrainNumberItem>({
  getPageUrl: Api.trainNumberPage,
  cudUrl: Api.trainNumbers,
});

function showCreateModal() {
  showOptions.value = { action: CURDAction.CREATE };
}
function showUpdateModal(record) {
  showOptions.value = { action: CURDAction.UPDATE, record };
}
function handleDelete(record) {
  showOptions.value = { action: CURDAction.DELETE, ids: [record.id] };
}
function beforeOk(e: CURDModalEvent<Partial<TrainNumberItem>>) {}
function open(e: CURDModalEvent<Partial<TrainNumberItem>>) {}
</script>
